# -*- coding: utf-8 -*-
"""
Created on Thu Dec  3 18:55:10 2020

@author: kanwa
"""

import time

from selenium import webdriver

driver = webdriver.Chrome('./diver/chromedriver')
try:
    #
    driver.get("http://www.python.org")
    driver.set_window_size(1190, 690)
    time.sleep(1)
    print(driver.title)
    # <button type="submit" name="submit" id="submit" class="search-button" title="Submit this Search" tabindex="3">
    #                                     GO
    #                                 </button>
    # 通过XPath定位元素
    # 通过形式 find_element_by_xpath("//标签名[@属性='属性值']")
    # element = driver.find_element_by_xpath("//button[@id='submit']")
    # element = driver.find_element_by_xpath("//button[@class='search-button']")
    # element = driver.find_element_by_xpath("//button[@name='submit']")
    # element = driver.find_element_by_xpath("//button[@title='Submit this Search']")
    # element = driver.find_element_by_xpath("//button")
    # <fieldset title="Search Python.org">
    #                                 <span aria-hidden="true" class="icon-search"></span>
    #                                 <label class="screen-reader-text" for="id-search-field">Search This Site</label>
    #                                 <input id="id-search-field" name="q" type="search" role="textbox" class="search-field" placeholder="Search" value="" tabindex="1">
    #                                 <button type="submit" name="submit" id="submit" class="search-button" title="Submit this Search" tabindex="3">
    #                                     GO
    #                                 </button>
    #                                 <!--[if IE]><input type="text" style="display: none;" disabled="disabled" size="1" tabindex="4"><![endif]-->
    # </fieldset>

    # element = driver.find_element_by_xpath("//fieldset/button")
    # <button type="submit" name="submit" id="submit" class="search-button" title="Submit this Search" tabindex="3">
    #                                     GO
    #                                 </button>
    # element = driver.find_element_by_xpath("//button[contains(text(),'GO')]")
    # element = driver.find_element_by_xpath("//button[contains(@class,'search-button')]")
    # element = driver.find_element_by_xpath("//fieldset/button[@class='search-button' and @id='submit']")
    # element = driver.find_element_by_xpath('//*[@id="submit"]')
    # element = driver.find_element_by_xpath('/html/body/div/header/div/div[1]/div/form/fieldset/button')

    # / html / body / div[1] / div[1] / div[5] / div / div / form / span[1] / input
    # 通过tag定位元素
    # element = driver.find_element_by_tag_name('/html/body/div[1]/header/div/div[1]/div/form/fieldset/input')
    element = driver.find_element_by_xpath('/html/body/div[1]/header/div/div[1]/div/form/fieldset/input')
    print("id:", element.get_attribute("id"))
    print("name:", element.get_property("name"))
    print("class:", element.get_attribute("class"))
    print("Text:", element.text)
    element.click()
    element.send_keys('max')
    time.sleep(1)
except Exception as e:
    print(e)
finally:
    driver.quit()
